package com.code;

import java.util.ArrayList;

import com.Global.Global;
import com.example.R;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class DetailCustomerForTMH extends Activity{
	
	private ArrayList<String> detail;
	ArrayList<Integer> date;
	private TextView tvPhone, tvDiaChi, tvMaSP, tvNgay, tvTienBan,tvSoLuong,tvNgayTruoc,tvSoNgay,tvNgayTiep,tvMaSo,tvThanhTien,tvDuNoCuoiKy,tvNgayBanGanNhat;
	private Button btTroVe, btCall;
	private String strPhoneNumber;
	ImageView imgWarning ;
	String strNhomChon = "",strNhomKho = "";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detailcuocgoi);
		
		//declare component
		tvMaSo =  (TextView)findViewById(R.id.tvMaSo);
		tvPhone = (TextView)findViewById(R.id.tvPhone);
		tvDiaChi = (TextView)findViewById(R.id.tvDiaChi);
		tvMaSP = (TextView)findViewById(R.id.tvMaSP);
		tvNgay = (TextView)findViewById(R.id.tvNgay);
		tvTienBan = (TextView)findViewById(R.id.tvTienBan);
		tvSoLuong = (TextView)findViewById(R.id.tvSoLuong);
		tvNgayTruoc= (TextView)findViewById(R.id.tvNgayTruoc);
		tvSoNgay = (TextView)findViewById(R.id.tvSoNgay);
		tvNgayTiep = (TextView)findViewById(R.id.tvNgayTiep);
		imgWarning = (ImageView)findViewById(R.id.imgWarning);
		tvThanhTien = (TextView)findViewById(R.id.tvThanhTien);
		tvDuNoCuoiKy = (TextView)findViewById(R.id.tvDuNoCuoiKy);
		btTroVe = (Button)findViewById(R.id.btTroVe);
		btCall = (Button)findViewById(R.id.btCall);
		tvNgayBanGanNhat = (TextView) findViewById(R.id.tvNgayBanGanNhat);
		
		detail = super.getIntent().getExtras().getStringArrayList("detailCustomer");
		date = super.getIntent().getExtras().getIntegerArrayList("dateDetailTMH");
		strNhomKho = super.getIntent().getExtras().getString("strKhoDetailTMH");
		strNhomChon = super.getIntent().getExtras().getString("strNhomChonDetailTMH");
		
		tvMaSo.setText(detail.get(4));
		tvPhone.setText(detail.get(7));
		strPhoneNumber = detail.get(7).toString();
		this.setTitle(detail.get(3));
		tvDiaChi.setText(detail.get(5));
		tvMaSP.setText(detail.get(0));
		tvNgay.setText(detail.get(6));
		tvTienBan.setText(Global.formatDecimal(Float.valueOf(detail.get(2))));
		tvSoLuong.setText(detail.get(9));
		// ngay truoc and chu ky
		String ngayTruoc = "", ngayKeTiep = "";
		Integer chuky = 0;
		if (DSCuocGoi.getMaxDateShipmentCustomer(detail.get(6),
				detail.get(4)) != null) {
			if (DSCuocGoi.getMaxPreviousDateOfMaxDateShipmentCustomer(
					detail.get(6), detail.get(4)) != null) {
				ngayTruoc = DSCuocGoi.getMaxPreviousDateOfMaxDateShipmentCustomer(
						detail.get(6), detail.get(4))
						.toString();
				chuky = DSCuocGoi.getChuKyLayHang(
						DSCuocGoi.getMaxPreviousDateOfMaxDateShipmentCustomer(
								detail.get(6), detail.get(4)),
								DSCuocGoi.getMaxDateShipmentCustomer(
								detail.get(6), detail.get(4)));
			}
			if(chuky > 0)
				ngayKeTiep = DSCuocGoi.ngayKeTiep(chuky,DSCuocGoi.getMaxDateShipmentCustomer(detail.get(6),  detail.get(4))).toString();

		}
	
		tvNgayTruoc.setText(ngayTruoc);
		tvSoNgay.setText(chuky.toString());
		if(!ngayKeTiep.equals("") && DetailCuocGoi.checkDate(ngayKeTiep, date))
		{
			tvNgayTiep.setText(ngayKeTiep);		
			imgWarning.setVisibility(View.GONE);
		}
		else
			tvNgayTiep.setText(ngayKeTiep);
		//du no cuoiky
		
		tvDuNoCuoiKy.setText(detail.get(8));
		//tong thanh tien
		tvThanhTien.setText(detail.get(10));
		tvNgayBanGanNhat.setText(detail.get(11).equals("") ? detail.get(11):Global.fomartDate(detail.get(11)));
		btCall.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_CALL);
				intent.setData(Uri.parse("tel:"+strPhoneNumber+""));
				startActivity(intent);
			}
		});
		
		btTroVe.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				setResult(RESULT_CANCELED);
				finish();
			}
		});
	}	

}
